package com.wenli.graduate2022.wenliGreenShop.repository.mapper;

import com.wenli.graduate2022.wenliGreenShop.entity.ProductImage;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author HuangHai
 * @date 2021/10/12 15:49
 */
@Repository
public interface ProductImageMapper {

    @Results(id = "productImage",value={
            @Result(property = "productImageProduct",column = "productimage_product_id",//根据数据库中的product_category_id字段作为条件，将查到的数据封装到productCategory属性里
                    one = @One(select = "com.wenli.graduate2022.wenliGreenShop.repository.mapper.ProductMapper.findById",
                            fetchType = FetchType.LAZY))//懒加载，除非详细查看，如调用get方法，才会进行sql执行（打印对象其实是只要重写toString方法也是调用了get方法）
    })
    @Select("select * from productImage where productimage_product_id = #{productId} and productimage_type = #{type}")
    List<ProductImage> findByType( Integer productId,Integer type);  //根据产品的Id,与图片类型查看所有符合条件的图片


    @Select("select * from productImage where productimage_product_id = #{productId}")
    @ResultMap("productImage")
    List<ProductImage> findById(Integer productId);  //查询该产品的所有图片

    @Select("select * from productImage where productimage_product_id in (select distinct product_id from product p" +
            " inner join category c where p.product_category_id = #{categoryId}) GROUP BY  productimage_product_id")
    @ResultMap("productImage")
    List<ProductImage> findByCategoryId(Integer categoryId);



}
